package com.company.笔试.链表反转排序;

/**
 * @Classname Sulotion
 * @Description TODO
 * @Date 2021/8/11 20:55
 * @Created by ylr
 */


import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */




public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    public static ListNode formatList (ListNode head) {
        // write code here
        if(head==null || head.next==null || head.next.next==null) return head;
        System.out.println("-");
        ListNode init = head;//1
        ListNode tt = head.next;//2
        ListNode t = head.next;//2
        ListNode hh = head.next.next;//3
        ListNode h = head.next.next;//3
        ListNode cur = head.next.next.next;//遍历指针
        while(cur!=null){
            t.next = cur;
            t = t.next;
            h.next = cur.next;
            h = h.next;
            cur = cur.next.next;
        }
        t.next = null;
        t = t.next;
        init.next = tt;
        ListNode tmp = null;
        ListNode tail = null ;
        ListNode hhg = hh ;

        while(hh!=null){
            tmp = hh.next;
            hh.next = tail;
            tail=hh;
            hh = tmp;

        }

        hhg.next = init;
        return tail;


    }

    public static void main(String[] args) {
        ListNode head =  new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        ListNode node = formatList(head);
        while (node!=null){
            System.out.println(node.val);
            node=node.next;
        }
    }
}

